import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class InteractiveCourseSystem {
    private static List<Teacher> teachers = new ArrayList<>();
    private static List<Student> students = new ArrayList<>();
    private static List<Course> courses = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        // 初始化数据
        initData();

        // 显示系统菜单
        showMainMenu();
    }

    // 初始化测试数据
    private static void initData() {
        // 添加教师
        teachers.add(new Teacher("T001", "张世博", "男"));
        teachers.add(new Teacher("T002", "余有名", "男"));
        teachers.add(new Teacher("T003", "赵娜娜", "女"));
        teachers.add(new Teacher("T004", "张秀芬", "女"));
        teachers.add(new Teacher("T005", "郭丹曦", "女"));
        teachers.add(new Teacher("T006", "陈爽", "女"));

        // 添加课程
        courses.add(new Course("C001", "Java编程", "主楼 2501", "周四 8:00-10:00"));
        courses.add(new Course("C002", "数据结构", "实验楼 0921", "周四 10:00-12:00"));
        courses.add(new Course("C003", "线性代数", "教学楼 102", "周一 14:00-16:00"));
        courses.add(new Course("C004", "太极拳", "主校区操场", "周三 14:00-16:00"));
        courses.add(new Course("C005", "红楼梦研读", "教学楼 302", "周五 19:00-21:00"));
        courses.add(new Course("C006", "高等数学", "教学楼 203", "周二 14:00-16:00"));

        // 分配教师到课程
        courses.get(0).setTeacher(teachers.get(0));
        courses.get(1).setTeacher(teachers.get(1));
        courses.get(2).setTeacher(teachers.get(2));
        courses.get(3).setTeacher(teachers.get(3));
        courses.get(4).setTeacher(teachers.get(4));
        courses.get(5).setTeacher(teachers.get(5));

        teachers.get(0).setCourse(courses.get(0));
        teachers.get(1).setCourse(courses.get(1));
        teachers.get(2).setCourse(courses.get(2));
        teachers.get(3).setCourse(courses.get(3));
        teachers.get(4).setCourse(courses.get(4));
        teachers.get(5).setCourse(courses.get(5));

        // 添加学生
        students.add(new Student("S001", "小明", "男"));
        students.add(new Student("S002", "小红", "女"));
        students.add(new Student("S003", "小刚", "男"));
        students.add(new Student("S004", "小丽", "女"));
    }

    // 显示主菜单
    private static void showMainMenu() {
        while (true) {
            System.out.println("\n===== 学生选课系统 =====");
            System.out.println("1. 登录系统");
            System.out.println("2. 退出系统");
            System.out.print("请选择操作(1-2): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消费换行符

            switch (choice) {
                case 1:
                    login();
                    break;
                case 2:
                    System.out.println("谢谢使用，再见！");
                    scanner.close();
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    // 登录功能
    private static void login() {
        System.out.print("请输入你的学生ID: ");
        String studentId = scanner.nextLine();

        // 查找学生
        Student currentStudent = null;
        for (Student s : students) {
            if (s.getId().equals(studentId)) {
                currentStudent = s;
                break;
            }
        }

        if (currentStudent == null) {
            System.out.println("找不到该学生ID，请重新尝试！");
            return;
        }

        System.out.println("登录成功！欢迎你，" + currentStudent.getName() + "同学！");
        showStudentMenu(currentStudent);
    }

    // 显示学生操作菜单
    private static void showStudentMenu(Student student) {
        while (true) {
            System.out.println("\n===== 学生操作菜单 =====");
            System.out.println("1. 查看可选课程");
            System.out.println("2. 选择课程");
            System.out.println("3. 退选课程");
            System.out.println("4. 查看已选课程");
            System.out.println("5. 退出登录");
            System.out.print("请选择操作(1-5): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消费换行符

            switch (choice) {
                case 1:
                    showAllCourses();
                    break;
                case 2:
                    selectCourse(student);
                    break;
                case 3:
                    dropCourse(student);
                    break;
                case 4:
                    showSelectedCourse(student);
                    break;
                case 5:
                    System.out.println("已退出登录！");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    // 显示所有可选课程
    private static void showAllCourses() {
        System.out.println("\n===== 所有可选课程 =====");
        for (Course course : courses) {
            System.out.println(course.id);
            System.out.println(course.name);
            System.out.println(course.location);
            System.out.println(course.time);
            System.out.println("-------------------");
        }
    }

    // 选课功能
    private static void selectCourse(Student student) {
        if (student.getCourse() != null) {
            System.out.println("你已经选过课程：" + student.getCourse().getName() + "，请先退课再重新选择！");
            return;
        }

        System.out.print("请输入要选择的课程ID: ");
        String courseId = scanner.nextLine();

        // 查找课程
        Course selectedCourse = null;
        for (Course c : courses) {
            if (c.getId().equals(courseId)) {
                selectedCourse = c;
                break;
            }
        }

        if (selectedCourse == null) {
            System.out.println("找不到该课程ID！");
            return;
        }

        student.selectCourse(selectedCourse);
        System.out.println("选课成功！你选择了：" + selectedCourse.getName());
    }

    // 退课功能
    private static void dropCourse(Student student) {
        if (student.getCourse() == null) {
            System.out.println("你没有选择任何课程，无需退课！");
            return;
        }

        String courseName = student.getCourse().getName();
        student.dropCourse();
        System.out.println("退课成功！你已退选：" + courseName);
    }

    // 查看已选课程
    private static void showSelectedCourse(Student student) {
        Course course = student.getCourse();
        if (course == null) {
            System.out.println("你当前没有选择任何课程！");
        } else {
            System.out.println("\n===== 你的已选课程 =====");
            System.out.println(course.id);
            System.out.println(course.name);
            System.out.println(course.location);
            System.out.println(course.time);
        }
    }
}